<template>
  <div class="module_container">
    <div class="module_title right" flex>
      <div class="title_text">重点企业</div>
    </div>
    <div class="content">
      <div class="sub_title" flex="main:justify cross:center">
        <div>
          今年国家高新企业<span style="
            color: #39fff5;
            font-size: 52px;
            padding:0 10px
          ">{{ totalObj.gTotal }}</span><span>家</span>
        </div>
        <div flex style="">
          <div class="btn" :class="{active:active === 'category'}" @click="active = 'category',initCharts()">按年份</div>
          <div class="btn" :class="{active:active === 'year'}" @click="active = 'year',initCharts()">按行业</div>
        </div>
      </div>
      <div ref="bar" class="bar" />
      <dv-decoration-2 style="width: 100%; height: 5px" />
      <dv-decoration-2 style="width: 100%; height: 5px; transform: rotate(180deg)" />
      <div class="sub_title">
        今年西兴街道重点企业<span style="
            font-size: 52px;
            color: #39fff5;
            padding:0 10px
          ">{{ totalObj.iTotal }}</span><span>家</span>
      </div>
      <div ref="bar1" class="bar" />
      <!-- <img src="@/assets/img_xinyong/module2/data_circle_bg.png" alt="" srcset=""> -->
    </div>
  </div>
</template>

<script>
import { optionBar } from './module4_option'
import { hitechKeyCategory } from '@/api/home'
import { groupBy } from '@/utils/index'
import Highcharts from '@/config/highCharts'
export default {
  data() {
    return {
      totalObj: { gTotal: 0, iTotal: 0 },
      active: 'category'
    }
  },
  watch: {
    // active (newValue) {

    // }
  },
  created() {
    // hitechKeyCategory().then(res => {
    //   this._seriesData1 = res.data.filter(item => item.level === 1)
    //   this._seriesData2 = res.data.filter(item => item.level === 2)
    //   this.initCharts()
    //   // this.handleChartsByYear()
    // })
    this.initCharts()
  },
  methods: {
    initCharts() {
      this.$nextTick(() => {
        // let obj = this.handleChartsByYear(this._seriesData1)
        // optionBar.series = obj.series
        // optionBar.xAxis.categories = obj.categories
        // this.totalObj.gTotal = obj.total || this.totalObj.gTotal
        Highcharts.chart(this.$refs.bar, optionBar)

        // obj = this.handleChartsByYear(this._seriesData2)
        // optionBar.series = obj.series
        // optionBar.xAxis.categories = obj.categories
        // this.totalObj.iTotal = obj.total || this.totalObj.iTotal
        Highcharts.chart(this.$refs.bar1, optionBar)
      })
    },
    handleChartsByYear(seriesData) {
      const currYear = new Date().getFullYear() - 1
      const obj = groupBy(seriesData, this.active)
      let categories = null

      if (this.active === 'category') {
        categories = Array.from({ length: 4 }).map((e, i) => currYear - 3 + i)
      } else {
        categories = Object.keys(groupBy(seriesData, 'category'))
      }
      const series = Object.keys(obj).map(key => {
        const yearArr = [...categories]
        return {
          name: key,
          data: yearArr.map(year => (obj[key].find(item => item[this.active === 'category' ? 'year' : 'category'] === year) || {}).num || 0)
        }
      })

      const total = this.active === 'category' ? series.reduce((prev, curr) => {
        return (prev + curr.data[curr.data.length - 1])
      }, 0) : 0
      return { series, categories, total }
      // optionBar.series = series
    },
    handleChartsByIndustry() {
      // optionBar.xAxis.categories = Object.keys(this._seriesData1)
      // const series = Array.from({ length: 4 }).map((e, i) => {
      //   const yearArr = [...optionBar.xAxis.categories]
      //   return {
      //     name: key,
      //     data: yearArr.map(year => (this._seriesData1[key].find(item => item.year === year) || {}).num || 0),
      //   }
      // })
    }
  }
}
</script>

<style lang="scss" scoped>
.content {
  // margin-top: 20px;
  // position: relative;
  padding: 25px 40px;
  font-size: 28px;
  .sub_title {
    margin-top: 20px;
    margin-bottom: 20px;

    background: url("~@/assets/img_xinyong/module6/sub_title_bg.png") no-repeat;
    width: 100%;
    height: 58px;
    line-height: 58px;
    // padding:20px;
    padding-left: 70px;
    font-size: 28px;
    .btn {
      padding: 3px 20px;
      border: 1px solid #f0f0f0;
      cursor: pointer;
      &.active,
      &:hover {
        background: rgba($color: #fff, $alpha: 0.2);
      }
    }
  }
  & > .bar {
    width: 100%;
    height: 240px;
    margin-bottom: 15px;
  }
  .box_1 {
    color: #bfdaff;
    .dight {
      background: url("~@/assets/img_qingqin/module5/dight_bg.png") no-repeat;
      background-size: 100% 100%;
      width: 120px;
      height: 66px;
      line-height: 52px;
      text-shadow: 0 16px 23px rgba(255, 203, 53, 0.5);
      font-size: 52px;
      color: #fad900;
      text-align: center;
      padding: 20px;
    }
    .dight_normal {
      height: 106px;
      line-height: 100px;
    }
  }

  .legend_box {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    position: relative;
    flex: 1;
    & > * {
      width: 48%;
      line-height: 64px;
      color: #bfdaff;
      background: url("~@/assets/img_qingqin/module2/card_bg.png") no-repeat;
      background-size: 100% 100%;
      text-align: center;
      padding: 0 10px;
      box-sizing: border-box;
      margin-bottom: 24px;
    }
  }
  & > .ranking {
    margin-bottom: 10px;
    color: #c0e6ff;
    & > *:first-child {
      width: 64px;
      height: 64px;
      text-align: center;
      line-height: 64px;
      background: url("~@/assets/img_qingqin/module2/rank_mark_normal_bg.png")
        no-repeat;
      background-size: 100% 100%;
      &.active {
        background: url("~@/assets/img_qingqin/module2/rank_mark_active_bg.png")
          no-repeat;
        background-size: 100% 100%;
      }
    }
    & > *:last-child {
      background: url("~@/assets/img_qingqin/module2/rank_data_bg.png")
        no-repeat;
      background-size: 100% 100%;
      width: 1020px;
      height: 64px;
      line-height: 64px;
      padding-left: 30px;
      &.active {
        background: url("~@/assets/img_qingqin/module2/rank_data_bg_active.png")
          no-repeat;
        background-size: 100% 100%;
      }
    }
  }
  .l_circle {
    background: url("~@/assets/img_xinyong/module2/data_circle_bg.png")
      no-repeat;
    background-size: cover;
    height: 280px;
    width: 280px;
    position: relative;
    .text {
      position: absolute;
      bottom: -50px;
      text-align: center;
      width: 100%;
    }
    // padding-bottom: 20px;
  }
  .list_box {
    color: #bfdaff;
    & > *:not(:first-child) {
      margin-top: 23px;
    }
  }
}
</style>
